package com.ask.resources;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.log4j.Level;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author suryakencana
 */
public class LoggerInf {

    private static LoggerInf logger = new LoggerInf();
    org.apache.log4j.Logger log4jLogger;
    private static final String FQCN = LoggerInf.class.getName();
    private String confLog = "smslib";
    private final String appPath = System.getProperty("user.dir");

    public LoggerInf() {
        if (System.getProperties().getProperty("java.vm.name").equalsIgnoreCase("ikvm.net")) {
            File f = new File(appPath + "/conf/" + "log4j.properties");
            URL url = null;
            if (!f.exists()) {
                log4jLogger = org.apache.log4j.Logger.getLogger(confLog);
                url = LoggerInf.class.getResource("/com/ask/resources/log4j.properties");
                System.err.println("---LogInf----");
            } else {
                try {
                    url = new URL(f.getPath());
                    log4jLogger = org.apache.log4j.Logger.getLogger(confLog);
                } catch (MalformedURLException ex) {
                    System.err.println(ex.getMessage());
                }
            }

            PropertyConfigurator.configure(url);
        } else {
            log4jLogger = org.apache.log4j.Logger.getLogger(confLog);
            PropertyConfigurator.configure(LoggerInf.class.getResource("/com/ask/resources/log4j.properties"));
             System.err.println("---LogInf!!!----");
        }
        System.err.println("---LogInf----"+confLog+"||||");
    }

    public static LoggerInf getInstance() {
        if (LoggerInf.logger == null) {
            LoggerInf.logger = new LoggerInf();
        }
        return LoggerInf.logger;
    }

    public static LoggerInf getInstance(String logger) {
        LoggerInf.logger.confLog = logger;
        if (LoggerInf.logger == null) {
            LoggerInf.logger = new LoggerInf();
        }
        return LoggerInf.logger;
    }

    public static void setInstance(LoggerInf logger) {
        LoggerInf.logger = logger;
    }

    public void logInfo(String message, Exception e, String gatewayId) {
        if (log4jLogger == null) {
            return;
        }
        log4jLogger.log(FQCN, Level.INFO, formatMessage(message, gatewayId), e);
    }

    public void logWarn(String message, Exception e, String gatewayId) {
        if (log4jLogger == null) {
            return;
        }
        log4jLogger.log(FQCN, Level.WARN, formatMessage(message, gatewayId), e);
    }

    public void logDebug(String message, Exception e, String gatewayId) {
        if (log4jLogger == null) {
            return;
        }
        log4jLogger.log(FQCN, Level.DEBUG, formatMessage(message, gatewayId), e);
    }

    public void logError(String message, Exception e, String gatewayId) {
        if (log4jLogger == null) {
            return;
        }
        log4jLogger.log(FQCN, Level.ERROR, formatMessage(message, gatewayId), e);
    }

    private String formatMessage(String message, String gatewayId) {
        return ((gatewayId == null) ? message : "GTW: " + gatewayId + ": " + message);
    }
}